<template>
  <div>
    <AplidForm
      ref="formRef"
      v-bind="BaseFormConfig"
      v-model="formModel"
      :key="key"
      @submit="onSubmit">
    </AplidForm>
  </div>
</template>

<script setup lang="ts">
import { BaseFormConfig } from '@/views/datacenter/basicData/serviceFacility/config'
import { getAreaObj } from '@/utils/util'
import { cloneDeep } from 'lodash-es'
import { useCommonStore } from '@/stores/common'
import { useOperate } from '@/views/datacenter/hooks/useConfig'

const { getValue } = useOperate(3)
const { setTempParams } = useCommonStore()
const rowData: any = inject('nowRowData')
let key = ref('1')
let dpServiceFacilityObject = rowData.value?.data?.dpServiceFacilityObject
// 处理出所属区域
if(dpServiceFacilityObject){
  const areasObj: any = getAreaObj(dpServiceFacilityObject, '', 'set')
  dpServiceFacilityObject.areas = areasObj.areaIds
}

const emit = defineEmits(['submit'])
let baseData = rowData.value?.baseData?.dpServiceFacilityObject
// 此处应当处理一下区域

const formModel = ref({
  ...cloneDeep(dpServiceFacilityObject)
})
formModel.value.unitNameCreditCode = [formModel.value.unitName,formModel.value.creditCode].join('-')
key.value = '2'
// dpServiceFacilityObject
// dpFacilityEquipmentList
// dpOperateRecordList
// dpSenileProductsList
// dpServiceFacilityExtend
// dpServiceFacilityOther

const formRef = ref(null)
const onSubmit = (form, done) => {
  const areaObj: any = getAreaObj(form.areas,'')
  const areaName: any = getAreaObj(form.areas_name,'')
  const params = {
      ...form,
      ...areaObj,
      ...areaName,
      code:2002,
      unitNameCreditCode:undefined
  }
  if(form.dailyDeliveryOfFoodAid && Array.isArray(form.dailyDeliveryOfFoodAid)) {
    params.dailyDeliveryOfFoodAid = form.dailyDeliveryOfFoodAid.join(',')
  }
  // 变更字段
  let changeColumnsBase = BaseFormConfig.column.filter(
    (item) => item.changeField
  )
  // 处理变更数据
  let changeData = []
  changeColumnsBase.map((item) => {
    const tempBefore = baseData[item.propName || item.prop]
    const tempAfter = params[item.propName || item.prop]
    if (tempBefore != tempAfter) {
      changeData.push({
        label: item.label,
        before: getValue(tempBefore, item),
        after: getValue(tempAfter, item)
      })
    }
  })
  const changeObj = {
    data: changeData,
    dataChangeContentList: [
      {
        changeBefore: {
          ...baseData,
          areas: undefined,
          areas_name:undefined,
          code: undefined
        },
        changeAfter: {
          ...params,
          code: undefined,
          areas: undefined,
          areas_name:undefined,
        },
        changeData: {
          ...params,
          areas: undefined,
          areas_name:undefined,
          code:2002,
        },
        changeType:'养老服务设施',
        code:2002,
      }
    ],
  }
  setTempParams({
    name: params.name,
    nowChange: changeObj
  })
  emit('submit', changeObj)
  done()
}

const onSave = () => {
  formRef.value!.onSubmit()
}

defineExpose({
  onSave
})
</script>

<style scoped></style>
